﻿using AutoMapper;
using Enter.Admin.Dtoes;
using Enter.Admin.Entities;
using Enter.Admin.Filters;
using Enter.Admin.Models;
using Enter.Admin.Models.DataTables;
using Enter.Admin.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Enter.Admin.Controllers
{
    [TypeFilter(typeof(ActionFilter), Arguments = new object[] { ActionRole.Admin })]
    public class RoleController : BaseController
    {

        public RoleController(ILogger<RoleController> logger, IFreeSql fsql, IMapper mapper) : base(logger, fsql, mapper)
        {
        }
        // GET: RoleController
        public ActionResult Index()
        {
            var columns = Excel.Headers<RoleModel>();
            var model = columns.Select(t => new { data = t.PropertyName, title = t.HeaderName, orderable = false, visible = true, searchable = false }).ToList();
            ViewData["columns"] = Jsons.Serialize(model);
            return View();
        }

        [HttpPost]
        // [ValidateAntiForgeryToken]
        public IActionResult Index([FromBody] DataTablesParameters<object> model)
        {
            var data = _fsql.Select<Role>().Where(t => t.IsDeleted == false).Count(out var total).Skip(model.Start).Take(model.Length).ToList();
            return Json(new DataTablesResult<Role>(model.Draw, total, total, data));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(RoleModel role)
        {
            if (_fsql.Select<Role>().Where(t => t.RoleName == role.RoleName && t.IsDeleted == false).Any())
            {
                return Json(new ResponseResult<string>(false, "角色已存在."));
            }
            var new_role = new Role()
            {
                Id = 0,
                RoleName = role.RoleName,
                IsDefault = false,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                IsDeleted = false,
            };
            _fsql.Insert<Role>(new_role).ExecuteAffrows();
            return Json(new ResponseResult<string>(true, "创建成功."));
        }

        // GET: RoleController/Edit/5
        public ActionResult Edit(int id)
        {
            if (id == 0)
            {
                return View(new Role());
            }
            var model = _fsql.Select<Role>(new { Id = id }).ToOne();
            if (model == null || model.IsDeleted)
            {
                return View("Error", new ErrorViewModel() { StatusCode = 404 });
                // 404
            }
            return View(model);
        }

        // POST: RoleController/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, RoleModel role)
        {
            if (_fsql.Select<Role>().Where(t => t.Id != id && t.RoleName == role.RoleName && t.IsDeleted == false).Any())
            {
                return Json(new ResponseResult<string>(false, "角色已存在."));
            }
            _fsql.Update<Role>()
                .SetIf(!string.IsNullOrWhiteSpace(role.RoleName), a => a.RoleName, role.RoleName)
                .Set(a => a.UpdateTime, DateTime.Now)
                .Where(a => a.Id == id).ExecuteAffrows();
            return Json(new ResponseResult<string>(true, "更新成功."));
        }

        // POST: RoleController/Delete/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            _fsql.Update<Role>()
                .Set(a => a.UpdateTime, DateTime.Now)
                .Set(a => a.IsDeleted, true)
                .Where(a => a.Id == id && a.IsDefault == false).ExecuteAffrows();
            return Json(new ResponseResult<string>(true, "删除成功."));
        }
    }
}
